package br.ufpe.cin.load;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import br.cin.ufpe.algorithms.kmeans.SymbolicFuzzyKMeans;
import br.cin.ufpe.basic.IntervalAttribute;
import br.cin.ufpe.basic.SymbolicPattern;

public class Load {
	
	private static int numAttributes;


	public static List<SymbolicPattern> loadSymbolicFromFile(String fileName) {
		List<SymbolicPattern> patterns = new ArrayList<SymbolicPattern>();
		String line = "";
		
		try{
		
		File file = new File(fileName);
		FileReader reader = new FileReader(file);
		BufferedReader buffer = new BufferedReader(reader);

		do {
			line = buffer.readLine();
			if (line != null){
				SymbolicPattern pattern = new SymbolicPattern();
				StringTokenizer tokens = new StringTokenizer(line, ",");
				
				int count = tokens.countTokens();
				numAttributes = count;
				String token = "";
				
				for (int j = 0; j < count; j++) {
					token = tokens.nextToken();
					double low = Double.parseDouble(token);
					low = SymbolicFuzzyKMeans.trunc(low);
					IntervalAttribute attribute = new IntervalAttribute(low,0.0);
					pattern.addAttribute(attribute);
				}
				
				line = buffer.readLine();
				tokens = new StringTokenizer(line,",");
				for(IntervalAttribute attribute : pattern.getAttributes()){
					double up = Double.parseDouble(tokens.nextToken());
					up = SymbolicFuzzyKMeans.trunc(up);
					attribute.setRadius(up);
				}
				patterns.add(pattern);
			}
		} while (line != null);
		
		}catch (Exception e) {
			System.out.println("Arquivo mal-formado "+e.getMessage());
		}

		return patterns;
	}
	
	public static int getNumAttributes(){
		return numAttributes;
	}

}